//
//  CommentCell.m
//  Echo
//
//  Created by Cospring on 14/12/17.
//  Copyright (c) 2014年 sina. All rights reserved.
//

#import "CommentCell.h"

@implementation CommentCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)layoutSubviews
{
    if (self.comment != nil) {
        
        self.userNameLabel.text = self.comment.owner.screen_name;
//        self.tweetText.text = self.comment.text;
        self.tweetText.font = [UIFont fontWithName:@"Heiti SC" size:14];
        
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:self.comment.text];
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 5;
        style.lineBreakMode = NSLineBreakByCharWrapping;
        [text addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
        
        self.tweetText.attributedText = text;
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        formatter.dateFormat = @"EEE, MMM dd HH:mm:ss ZZZ yyyy";
        
        NSDate *date1 = [formatter dateFromString:self.comment.created_time];
        NSLog(@"%@", date1);
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date1];
        
        NSInteger year = [components year];
        NSInteger month = [components month];
        NSInteger day = [components day];
        
        NSString *dateString = [NSString stringWithFormat:@"%ld年%ld月%ld日", (long)year, (long)month, (long)day];
        
        self.tweetCreatedTime.text = dateString;
        
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.comment.owner.profile_image_url]];
        UIImage *image = [UIImage imageWithData:imageData];
        self.userImageView.image = image;
//        self.sparateImageView.hidden = YES;
        self.userImageView.layer.cornerRadius = 10;
        self.userImageView.layer.masksToBounds = YES;
        
        
        self.comment = nil;
    }
}

+ (CGFloat)heightForCommentCell:(Comment *)comment
{
    NSString *txt = comment.text;
//    CGSize sizeToFit = [txt sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(222.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    
    UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font = [UIFont fontWithName:@"Heiti SC" size:14];
    gettingSizeLabel.text = txt;
    gettingSizeLabel.numberOfLines = 0;
    gettingSizeLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize maximumLabelSize = CGSizeMake(222, 9999);
    
    CGSize expectSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
    
    return expectSize.height + 60;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
